<?php
/* 
 * @package Analyser
 * @author Yves Peeters
 */
class HttpHeaderAnalyser{
     /**
     * retrieves the expire date from http headers
     * @param String $url
     * @return String
     */
    static function expires($url) {
        if (substr($url,0,4)=='http') {
            $x = array_change_key_case(get_headers($url, 1),CASE_LOWER);
            if ( strcasecmp($x[0], 'HTTP/1.1 200 OK') != 0 ) {$x = $x['expires'][1];}
            else {$x = $x['expires'];}
        }
        else { $x = "0";}
        return $x;
    }

    /**
     * Retrieves the content encoding from http headers (gzip)
     * @param String $url
     * @return String
     */
    function content_encoding($url) {
        if (substr($url,0,4)=='http') {
            $x = array_change_key_case(get_headers($url, 1),CASE_LOWER);
            if ( strcasecmp($x[0], 'HTTP/1.1 200 OK') != 0 ) {
                $x = $x['content-encoding'][1];
            }
            else { $x = $x['content-encoding'];}
        }
        else {$x = "0";}
        return $x;
    }

    /**
     * retrieves the etag from http headers
     * @param String $url
     * @return String
     */
    function etag($url) {
        if (substr($url,0,4)=='http') {
            $x = array_change_key_case(get_headers($url, 1),CASE_LOWER);
            if ( strcasecmp($x[0], 'HTTP/1.1 200 OK') != 0 ) { $x = $x['etag'][1];}
            else { $x = $x['etag']; }
        }
        else {$x = "0";}
        return $x;
    }

      /* To get the modification date of some remote file, you can use the fine function by notepad at codewalker dot com
	(with improvements by dma05 at web dot de and madsen at lillesvin dot net).

	But you can achieve the same result more easily now with stream_get_meta_data (PHP>4.3.0).

	However a problem may arise if some redirection occurs. In such a case, the server HTTP response contains no Last-Modified header,<br />
	but there is a Location header indicating where to find the file. The function below takes care of any redirections, even multiple redirections, <br />
	so that you reach the real file of which you want the last modification date.
	http://php.net/manual/en/function.filemtime.php
    */


    function GetRemoteLastModified($uri) {
        // get remote file last modification date (returns unix timestamp)
        // default
        $unixtime = 0;

        $fp = fopen( $uri, "r" );
        if( !$fp ) {
            return;
        }
        $MetaData = stream_get_meta_data( $fp );
        foreach( $MetaData['wrapper_data'] as $response ) {
            // case: redirection
            if( substr( strtolower($response), 0, 10 ) == 'location: ' ) {
                $newUri = substr( $response, 10 );
                fclose( $fp );
                return $this->GetRemoteLastModified( $newUri );
            }
            // case: last-modified
            elseif( substr( strtolower($response), 0, 15 ) == 'last-modified: ' ) {
                $unixtime = strtotime( substr($response, 15) );
                break;
            }
        }
        fclose( $fp );
        return $unixtime;
    }

}
?>
